package com.zmx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmx.entity.SysLog;
import com.zmx.mapper.SysLogMapper;
import com.zmx.service.SysLogService;
import com.zmx.utils.RUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.util.List;

/**
 * 2020/10/22
 * 13:57
 * zmx
 */
@Service("sysLogService")
public class SysLogServiceImpl implements SysLogService {
    @Resource
    SysLogMapper sysLogMapper;
    @Override
    public int insertLog(SysLog sysLog) {
        return sysLogMapper.insert(sysLog);
    }

    @Override
    public RUtils findByPage(int limit, int offset, String order, String search) {
        QueryWrapper<SysLog> sysLogQueryWrapper = null;
        if (search != null) {
            sysLogQueryWrapper = new QueryWrapper<>();
            sysLogQueryWrapper.like("method",search );
        }
        if (order != null) {
            if (sysLogQueryWrapper == null) {
                sysLogQueryWrapper = new QueryWrapper<>();
                if ("desc".equals(order)){
                    sysLogQueryWrapper.orderByDesc("id");
                }else {
                    sysLogQueryWrapper.orderByAsc("id");
                }
            }
        }
        int current = offset/limit + 1;
        Page<SysLog> sysLogPage = new Page<SysLog>(current,limit);
        IPage<SysLog> sysLogIPage = sysLogMapper.selectPage(sysLogPage, null);
        List<SysLog> rows = sysLogIPage.getRecords();
        long total = sysLogIPage.getTotal();
        return RUtils.success("查询成功", null).put("total",total)
                .put("rows", rows);
    }

    @Override
    public RUtils deleteAll(List<Long> ids) {
        int i = sysLogMapper.deleteBatchIds(ids);
        return i>0?RUtils.success("删除成功",null):RUtils.fail("删除失败",null);
    }
}
